<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Flex 布局组件 | MyWeb 4.x</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/my/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c4e5f73318b5cb0c389e3d9a05f831cc";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <meta name="description" content="基于Vue + ElementUI 的web项目工程框架，专注于公检法行业中台系统快速搭建，框架已在多个项目实战检验。">
    <link rel="preload" href="/my/assets/css/0.styles.d7ae9199.css" as="style"><link rel="preload" href="/my/assets/js/app.2b2365a7.js" as="script"><link rel="preload" href="/my/assets/js/535.c100cd07.js" as="script"><link rel="preload" href="/my/assets/js/1360.50a15eb8.js" as="script"><link rel="preload" href="/my/assets/js/1218.ee17b6fa.js" as="script"><link rel="preload" href="/my/assets/js/1025.8afc0690.js" as="script">
    <link rel="stylesheet" href="/my/assets/css/0.styles.d7ae9199.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/my/" class="home-link router-link-active"><img src="/my/img/logo.png" alt="MyWeb 4.x" class="logo"> <span class="site-name can-hide">MyWeb 4.x</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link router-link-active">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link router-link-active">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>功能辅助</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础元件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>排版布局</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my/ui/components/fixed.html" class="sidebar-link">Fixed 定位布局组件</a></li><li><a href="/my/ui/components/flex.html" aria-current="page" class="active sidebar-link">Flex 布局组件</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#flex-布局是什么？" class="sidebar-link">Flex 布局是什么？</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#基本概念" class="sidebar-link">基本概念</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#代码演示" class="sidebar-link">代码演示</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#排列方向" class="sidebar-link">排列方向</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#换行" class="sidebar-link">换行</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#主轴对齐" class="sidebar-link">主轴对齐</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#交叉轴对齐" class="sidebar-link">交叉轴对齐</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#多行对齐方式" class="sidebar-link">多行对齐方式</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#排序" class="sidebar-link">排序</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#放大比例" class="sidebar-link">放大比例</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#缩小比例" class="sidebar-link">缩小比例</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#basis" class="sidebar-link">basis</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#单独对齐" class="sidebar-link">单独对齐</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#适配父容器" class="sidebar-link">适配父容器</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#适配列数与边框" class="sidebar-link">适配列数与边框</a></li><li class="sidebar-sub-header"><a href="/my/ui/components/flex.html#阴影显示时机" class="sidebar-link">阴影显示时机</a></li></ul></li></ul></li><li><a href="/my/ui/components/float.html" class="sidebar-link">Float 浮动布局组件</a></li><li><a href="/my/ui/components/grid-layout.html" class="sidebar-link">GridLayout</a></li><li><a href="/my/ui/components/layout.html" class="sidebar-link">Layout 布局</a></li><li><a href="/my/ui/components/pro.html" class="sidebar-link">Pro 通用后台框架</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>容器修饰</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>导航菜单</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据录入</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据展示</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>动画效果</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="flex-布局组件"><a href="#flex-布局组件" class="header-anchor">#</a> Flex 布局组件</h1> <p>布局的传统解决方案，基于盒状模型，依赖 display 属性 + position属性 + float属性。它对于那些特殊布局非常不方便，比如，垂直居中就不容易实现。</p> <p>2009年，W3C 提出了一种新的方案----Flex 布局，可以简便、完整、响应式地实现各种页面布局。目前，它已经得到了所有浏览器的支持，这意味着，现在就能很安全地使用这项功能。</p> <p>Flex布局包含两个组件:
<a href="/my/api/components.html?url=%2Fmy%2Fapi%2Fcomponents%2Fmodule-%24ui_components_my-flex.html" target="_blank" data-v-3de95506>MyFlex <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-3de95506 data-v-3de95506><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>、
<a href="/my/api/components.html?url=%2Fmy%2Fapi%2Fcomponents%2Fmodule-%24ui_components_my-flex-item.html" target="_blank" data-v-3de95506>MyFlexItem <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-3de95506 data-v-3de95506><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="flex-布局是什么？"><a href="#flex-布局是什么？" class="header-anchor">#</a> Flex 布局是什么？</h2> <p>Flex 是 Flexible Box 的缩写，意为&quot;弹性布局&quot;，用来为盒状模型提供最大的灵活性。</p> <p>任何一个容器都可以指定为 Flex 布局。</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">.box</span><span class="token punctuation">{</span>
  <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>行内元素也可以使用 Flex 布局。</p> <div class="language-css line-numbers-mode"><pre class="language-css"><code><span class="token selector">.box</span><span class="token punctuation">{</span>
  <span class="token property">display</span><span class="token punctuation">:</span> inline-flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>注意，设为 Flex 布局以后，子元素的float、clear和vertical-align属性将失效。</p></div> <h2 id="基本概念"><a href="#基本概念" class="header-anchor">#</a> 基本概念</h2> <p>采用 Flex 布局的元素，称为 Flex 容器（flex container），简称&quot;容器&quot;。它的所有子元素自动成为容器成员，称为 Flex 项目（flex item），简称&quot;项目&quot;。</p> <p><img src="/xdh-web/img/flex.png" alt="flex"></p> <p>容器默认存在两根轴：水平的主轴（main axis）和垂直的交叉轴（cross axis）。主轴的开始位置（与边框的交叉点）叫做main start，结束位置叫做main end；交叉轴的开始位置叫做cross start，结束位置叫做cross end。</p> <p>项目默认沿主轴排列。单个项目占据的主轴空间叫做main size，占据的交叉轴空间叫做cross size。</p> <h2 id="代码演示"><a href="#代码演示" class="header-anchor">#</a> 代码演示</h2> <h3 id="排列方向"><a href="#排列方向" class="header-anchor">#</a> 排列方向</h3> <p>flex-direction属性决定主轴的方向（即项目的排列方向）。</p> <!----><h3 id="换行"><a href="#换行" class="header-anchor">#</a> 换行</h3> <p>默认情况下，项目都排在一条线（又称&quot;轴线&quot;）上。flex-wrap属性定义，如果一条轴线排不下，如何换行。</p> <ul><li>nowrap（默认）：不换行。</li> <li>wrap：换行，第一行在上方。</li> <li>wrap-reverse：换行，第一行在下方。</li></ul> <!----><h3 id="主轴对齐"><a href="#主轴对齐" class="header-anchor">#</a> 主轴对齐</h3> <p>justify-content属性定义了项目在主轴上的对齐方式。</p> <ul><li>flex-start（默认值）：左对齐</li> <li>flex-end：右对齐</li> <li>center： 居中</li> <li>space-between：两端对齐，项目之间的间隔都相等。</li> <li>space-around：每个项目两侧的间隔相等。所以，项目之间的间隔比项目与边框的间隔大一倍。</li></ul> <!----><h3 id="交叉轴对齐"><a href="#交叉轴对齐" class="header-anchor">#</a> 交叉轴对齐</h3> <p>align-items属性定义项目在交叉轴上如何对齐。</p> <ul><li>flex-start：交叉轴的起点对齐。</li> <li>flex-end：交叉轴的终点对齐。</li> <li>center：交叉轴的中点对齐。</li> <li>baseline: 项目的第一行文字的基线对齐。</li> <li>stretch（默认值）：如果项目未设置高度或设为auto，将占满整个容器的高度。</li></ul> <!----><h3 id="多行对齐方式"><a href="#多行对齐方式" class="header-anchor">#</a> 多行对齐方式</h3> <p>align-content属性定义了多根轴线（多行）的对齐方式。如果项目只有一根轴线，该属性不起作用</p> <ul><li>flex-start：交叉轴的起点对齐。</li> <li>flex-end：与交叉轴的终点对齐。</li> <li>center：与交叉轴的中点对齐。</li> <li>space-between：与交叉轴两端对齐，轴线之间的间隔平均分布。</li> <li>space-around：每根轴线两侧的间隔都相等。所以，轴线之间的间隔比轴线与边框的间隔大一倍。</li> <li>stretch（默认值）：轴线占满整个交叉轴。</li></ul> <!----><h3 id="排序"><a href="#排序" class="header-anchor">#</a> 排序</h3> <p>order属性定义项目的排列顺序。数值越小，排列越靠前，默认为0。</p> <!----><h3 id="放大比例"><a href="#放大比例" class="header-anchor">#</a> 放大比例</h3> <p>flex-grow属性定义项目的放大比例，默认为0，即如果存在剩余空间，也不放大。</p> <p>如果所有项目的flex-grow属性都为1，则它们将等分剩余空间（如果有的话）。如果一个项目的flex-grow属性为2，其他项目都为1，则前者占据的剩余空间将比其他项多一倍。</p> <!----><h3 id="缩小比例"><a href="#缩小比例" class="header-anchor">#</a> 缩小比例</h3> <p>flex-shrink属性定义了项目的缩小比例，默认为1，即如果空间不足，该项目将缩小。</p> <p>如果所有项目的flex-shrink属性都为1，当空间不足时，都将等比例缩小。如果一个项目的flex-shrink属性为0，其他项目都为1，则空间不足时，前者不缩小。</p> <p>负值对该属性无效。</p> <!----><h3 id="basis"><a href="#basis" class="header-anchor">#</a> basis</h3> <p>flex-basis属性定义了在分配多余空间之前，项目占据的主轴空间（main size）。浏览器根据这个属性，计算主轴是否有多余空间。它的默认值为auto，即项目的本来大小。</p> <!----><h3 id="单独对齐"><a href="#单独对齐" class="header-anchor">#</a> 单独对齐</h3> <p>align-self属性允许单个项目有与其他项目不一样的对齐方式，可覆盖align-items属性。默认值为auto，表示继承父元素的align-items属性，如果没有父元素，则等同于stretch。</p> <!----><h3 id="适配父容器"><a href="#适配父容器" class="header-anchor">#</a> 适配父容器</h3> <!----><h3 id="适配列数与边框"><a href="#适配列数与边框" class="header-anchor">#</a> 适配列数与边框</h3> <!----><h3 id="阴影显示时机"><a href="#阴影显示时机" class="header-anchor">#</a> 阴影显示时机</h3> <!----></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my/ui/components/fixed.html" class="prev">
        Fixed 定位布局组件
      </a></span> <span class="next"><a href="/my/ui/components/float.html">
        Float 浮动布局组件
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/my/assets/js/app.2b2365a7.js" defer></script><script src="/my/assets/js/535.c100cd07.js" defer></script><script src="/my/assets/js/1360.50a15eb8.js" defer></script><script src="/my/assets/js/1218.ee17b6fa.js" defer></script><script src="/my/assets/js/1025.8afc0690.js" defer></script>
  </body>
</html>
